package com.ny.controller;
import com.ny.entity.Reservations;
import com.ny.entity.Users;
import com.ny.service.ClassroomService;
import com.ny.service.ReservationService;
import com.ny.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpSession;
import java.util.List;
@Controller
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    private UserService userService;
    @Autowired
    private ReservationService reservationService;
    @Autowired
    private ClassroomService classroomService;
    @RequestMapping( "/adminLogin")
    public String adminLogin(String username, String password, Model model, HttpSession session){
        Users user =  userService.findUserByUsernameAndPassword(username,password);
        session.setAttribute("user",user);
        System.out.println(user);
        if(user!=null && user.getRole().equals("管理员")){
//            查询预约表的信息
            List<Reservations> reservationList =  reservationService.findAllReservations();
            model.addAttribute("reservationList",reservationList);
            return "reservations";
        }
        else{
            return "login";
        }

    }

//    通过
    @RequestMapping("/approveReservation")
    public String approveReservation(int reservationId,int classroomId, Model model){

        //            1.修改教室的状态
        classroomService.updateClassroomStatusById(classroomId,"已预约");
//      2.修改预约表的状态，还有驳回的原因
        reservationService.updateReservationStatus(reservationId,"通过",null);

        return "redirect:/reservations";

    }


//    不通过
    @RequestMapping("/rejectReservation")
    public String rejectReservation(int reservationId,int classroomId, String rejectReason, Model model){
//            1.修改教室的状态
        classroomService.updateClassroomStatusById(classroomId,"不通过");
//      2.修改预约表的状态，还有驳回的原因
        reservationService.updateReservationStatus(reservationId,"驳回",rejectReason);

        return "redirect:/reservations";

    }


}
